<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12">
					<el-form-item label="合同类别" prop="typeId">
						<el-tree-select
							v-model="dataForm.typeId"
							:data="heTongList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同标题" prop="titleStr">
						<el-input v-model="dataForm.titleStr" placeholder="合同标题"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="课件网范围" prop="isAll">
						<el-input v-model="dataForm.isAll" placeholder="课件网范围"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同编号" prop="contractNo">
						<el-input v-model="dataForm.contractNo" placeholder="合同编号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="签署人" prop="signatory">
						<el-input v-model="dataForm.signatory" placeholder="签署人"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同公司" prop="companyId">
						<el-tree-select
							v-model="dataForm.companyId"
							:data="deptList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="签署日期" prop="dateSigned">
						<ElDatePicker v-model="dataForm.dateSigned" placeholder="签署日期" style="width:100%"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="合同开始时间" prop="startDate">
						<ElDatePicker v-model="dataForm.startDate" placeholder="合同开始时间" style="width:100%"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="合同结束时间" prop="endDate">
				<ElDatePicker v-model="dataForm.endDate" placeholder="合同结束时间" style="width:100%"/>
			</el-form-item>
			<el-form-item label="合同附件" prop="fileUrl">
				<el-input v-model="dataForm.fileUrl" placeholder="合同附件"></el-input>
				<UploadImgView v-model="dataForm.fileUrl"></UploadImgView>
			</el-form-item>

			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { Ref, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useContractApi, useContractSubmitApi } from '@/api/nb/contract/contract'
import UploadImgView from '@/views/nb_module/image/upload-img-view.vue'
import { findListAll } from '@/api/nb/organization/organization'
import { findListByDictId } from '@/api/nb/business/dictionaries_subset'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	typeId: '',
	titleStr: '',
	isAll: '',
	contractNo: '',
	signatory: '',
	companyId: '',
	dateSigned: '',
	startDate: '',
	endDate: '',
	fileUrl: '',
	remark: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getContract(id)
	}
}

const getContract = (id: number) => {
	useContractApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const deptList=ref()
const heTongList=ref()
// 获取 上级组织
const getDeptTree = () => {
	return findListAll().then(res => {
		deptList.value = res.data
	})
}

const getDictList = (code: String, list: Ref) => {
	findListByDictId({ params: { code: code } }).then(res => {
		list.value = res.data
	})
}
getDictList('HeTongLeiBie', heTongList)
getDeptTree()

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useContractSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
